package com.pig4cloud.pig.module.controller.raffle;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.location.City;
import com.pig4cloud.pig.module.api.entity.location.Province;
import com.pig4cloud.pig.module.api.entity.raffle.RaffleActivity;
import com.pig4cloud.pig.module.service.location.CityService;
import com.pig4cloud.pig.module.service.location.ProvinceService;
import com.pig4cloud.pig.module.service.raffle.IRaffleActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 抽奖活动Controller
 *
 * @author ruoyi
 * @date 2021-08-29
 */
@Validated
@Api(value = "抽奖活动控制器", tags = {"抽奖活动管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/raffle")
public class RaffleActivityController {

    private final IRaffleActivityService raffleActivityService;
	private final ProvinceService provinceService;
	private final CityService cityService;

    /**
     * 查询抽奖活动列表
     */
    @ApiOperation("查询抽奖活动列表")
    @PreAuthorize("@pms.hasPermission('module:raffActive:list')")
    @GetMapping("/list")
    public TableDataInfo<RaffleActivity> list(@Validated RaffleActivity raffleActivity) {
        return PageUtils.buildDataInfo(raffleActivityService.page(PageUtils.buildPage()));
    }

    /**
     * 获取抽奖活动详细信息
     */
    @ApiOperation("获取抽奖活动详细信息")
    @PreAuthorize("@pms.hasPermission('module:raffActive:query')")
    @GetMapping("/{id}")
    public R<RaffleActivity> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        RaffleActivity raffleActivity = raffleActivityService.getById(id);
        if(raffleActivity!=null){
			if(raffleActivity.getProvId()!=null && raffleActivity.getCityId()!=null){
				String[] areas = { raffleActivity.getProvId()+"",raffleActivity.getCityId()+""};
				raffleActivity.setProvinceArray(areas);
			}
		}
        return R.ok(raffleActivity);
    }

    /**
     * 新增抽奖活动
     */
    @ApiOperation("新增抽奖活动")
    @PreAuthorize("@pms.hasPermission('module:raffActive:add')")
    @SysLog("抽奖活动")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody RaffleActivity raffleActivity) {

		if(raffleActivity.getProvinceArray()!=null&&raffleActivity.getProvinceArray().length==2){
			raffleActivity.setProvId(Integer.parseInt(raffleActivity.getProvinceArray()[0]));
			raffleActivity.setCityId(Integer.parseInt(raffleActivity.getProvinceArray()[1]));

			Province province = provinceService.findById(raffleActivity.getProvinceArray()[0]);
			if(province!=null){
				raffleActivity.setProvName(province.getProvinceName());
			}

			City city = cityService.findById(raffleActivity.getProvinceArray()[1]);
			if(city!=null){
				raffleActivity.setCityName(city.getCityName());
			}
		}

        return R.okByRow(raffleActivityService.save(raffleActivity) ? 1 : 0);
    }

    /**
     * 修改抽奖活动
     */
    @ApiOperation("修改抽奖活动")
    @PreAuthorize("@pms.hasPermission('module:raffActive:edit')")
    @SysLog("抽奖活动")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody RaffleActivity raffleActivity) {

		if(raffleActivity.getProvinceArray()!=null&&raffleActivity.getProvinceArray().length==2){
			raffleActivity.setProvId(Integer.parseInt(raffleActivity.getProvinceArray()[0]));
			raffleActivity.setCityId(Integer.parseInt(raffleActivity.getProvinceArray()[1]));

			Province province = provinceService.findById(raffleActivity.getProvinceArray()[0]);
			if(province!=null){
				raffleActivity.setProvName(province.getProvinceName());
			}

			City city = cityService.findById(raffleActivity.getProvinceArray()[1]);
			if(city!=null){
				raffleActivity.setCityName(city.getCityName());
			}
		}
        return R.okByRow(raffleActivityService.updateById(raffleActivity) ? 1 : 0);
    }

    /**
     * 删除抽奖活动
     */
    @ApiOperation("删除抽奖活动")
    @PreAuthorize("@pms.hasPermission('module:raffActive:remove')")
    @SysLog("抽奖活动")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(raffleActivityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
